package cn.chengpengper.rpc.common.scanner.reference;

import cn.chengpengper.rpc.annotation.RpcReference;
import cn.chengpengper.rpc.common.scanner.ClassScanner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author chengpeng.hu
 */
@Slf4j
public class RpcReferenceScanner extends ClassScanner {

    /**
     * 扫描指定包下的类，并筛选使用@RpcReference注解标注的类
     */
    public static Map<String, Object> doScanWithRpcReferenceAnnotationFilter(String scanPackage) {
        Map<String, Object> handlerMap = new HashMap<>(16);
        List<String> classNameList = getClassNameList(scanPackage);
        if (CollectionUtils.isEmpty(classNameList)){
            return handlerMap;
        }
        classNameList.forEach((className) -> {
            try {
                Class<?> clazz = Class.forName(className);
                Field[] declaredFields = clazz.getDeclaredFields();
                Stream.of(declaredFields).forEach((field) -> {
                    RpcReference rpcReference = field.getAnnotation(RpcReference.class);
                    if (rpcReference != null){
                        //TODO 处理后续逻辑，将@RpcReference注解标注的接口引用代理对象，放入全局缓存中
                        log.info("当前标注了@RpcReference注解的字段名称===>>> " + field.getName());
                        log.info("@RpcReference注解上标注的属性信息如下：");
                        log.info("version===>>> " + rpcReference.version());
                        log.info("group===>>> " + rpcReference.group());
                        log.info("registryType===>>> " + rpcReference.registryType());
                        log.info("registryAddress===>>> " + rpcReference.registryAddress());
                    }
                });
            } catch (Exception e) {
                log.error("scan classes throws exception: ", e);
            }
        });
        return handlerMap;
    }
}
